/* eslint-disable vue/no-unused-vars */
<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item label="所属学科" prop="subjectName">
      <el-select
        @change="changeOption"
        v-model="form.subjectName"
        placeholder="请选择目录名称"
      >
        <el-option
          v-for="item in arr"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="目录名称" prop="directoryName" style="width: 300px">
      <el-input v-model="form.directoryName"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="hCancel">取 消</el-button>
      <el-button @click="onSubmit">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { update, add, list } from '@/api/hmmm/directorys'
import { simple } from '@/api/hmmm/subjects.js'

export default {
  name: 'DirectorysAdd',
  props: {
    row: {
      type: Object,
      default: () => {}
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      dialogVisible: false,
      form: {
        region: '',
        directoryName: '',
        subjectName: '',
        isFrontDisplay: 0,
        subjectID: 0
      },
      rules: {
        subjectName: [
          { required: true, message: '所属学科不能为空', trigger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '目录名称不能为空', trigger: 'blur' }
        ]
      },
      arr: []
    }
  },
  created () {
    this.loadSimple()
    // console.log(this.row, 888888)
    this.wwww()
    // this.form.directoryName = this.row.directoryName
    // this.form.region = this.row.region
    // this.form.subjectName = this.row.subjectName
    // this.form.subjectID = this.row.subjectID
    // console.log(this.form, 3333)
  },
  watch: {
    isEdit: {
      handler (newValue) {
        // 浅拷贝
        if (!newValue) return
        this.form = { ...this.row }
        // 深拷贝
        // this.form = JSON.parse(JSON.stringify(this.row))
      },
      immediate: true
    }
  },

  methods: {
    async loadSimple () {
      const res = await simple()
      console.log(res, 666666)
      this.arr = res.data
      // console.log(this.arr, 44444444)
    },

    async wwww () {
      const res = await list()
      console.log(res, 9999999999999)
      // this.arr = res.data.items
      // console.log(this.arr, 777777)
    },

    async onSubmit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let res = {}
          if (!this.isEdit) {
            res = await add({
              ...this.form
            })
            if (res.status !== 200) return
            this.$message.success('恭喜你添加成功')
          } else {
            res = await update(this.form)
            // console.log(id);
            if (res.status !== 200) return
            // console.log(res, 3452351)
            this.$message.success('恭喜你修改学科成功')
          }

          // console.log(this.$parent.$parent.$parent.dialogVisible)
          // this.$parent.$parent.$parent.dialogVisible = false
          this.$emit('scucces')
        }
      })
    },
    hCancel () {
      this.$emit('update:visible', false)
    },
    changeOption (id) {
      // console.log(item)
      this.form.subjectID = id
    }
  }
}
</script>

<style lang="scss" scoped></style>
